// SinaAccessAuthoriseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PlatEngine.h"
#include "SinaAccessAuthoriseDlg.h"

// CSinaAccessAuthoriseDlg dialog

IMPLEMENT_DYNAMIC(CSinaAccessAuthoriseDlg, CDialog)

CSinaAccessAuthoriseDlg::CSinaAccessAuthoriseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSinaAccessAuthoriseDlg::IDD, pParent)
{

}

CSinaAccessAuthoriseDlg::~CSinaAccessAuthoriseDlg()
{
}

void CSinaAccessAuthoriseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EXPLORER1, m_oExplorer);
}


BEGIN_MESSAGE_MAP(CSinaAccessAuthoriseDlg, CDialog)
END_MESSAGE_MAP()


// CSinaAccessAuthoriseDlg message handlers

BOOL CSinaAccessAuthoriseDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	CString url(m_strReqAuthUrl.PtrZ());
	m_oExplorer.Navigate((LPCTSTR)url,NULL,NULL,NULL,NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}
BEGIN_EVENTSINK_MAP(CSinaAccessAuthoriseDlg, CDialog)
	ON_EVENT(CSinaAccessAuthoriseDlg, IDC_EXPLORER1, 252, CSinaAccessAuthoriseDlg::NavigateComplete2Explorer1, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()


void CSinaAccessAuthoriseDlg::NavigateComplete2Explorer1(LPDISPATCH pDisp, VARIANT* URL)
{
	CString str=URL->bstrVal;
	CRtString strUrl;
	ConvertUnicodeToUtf8(str.GetBuffer(),str.GetLength(),strUrl);
	str.ReleaseBuffer();

	int n=strUrl.Find("?code=");
	if(n>0)
	{
		m_strCode=strUrl.Mid(n+6);
		this->OnOK();
	}
	else 
	{
		n=strUrl.Find("?error_uri=");
		if(n>0)
			this->OnCancel();
	}
}
